import myVode from './myVode'
/**
 * myVode(sel, data, children, text, elm)
 *
 * h函数作用：生成一个虚拟的dom树形结构--处理传入的值的多少和类型
 * @param {String} sel 元素的标签名
 * @param {*} data 元素的属性
 * @param {*} c 元素的文本内容
 * 为了方便学习，此处的h函数只做了三种情况的考虑
 * ①h('dev',{}.'文字')
 * ②h('dev',{}.[])
 * ③h('dev',{}.h())
 */
export default function myH(sel, data, c) {
  if (arguments.length !== 3) {
    throw Error('必须传入三个参数')
  }
  if (typeof c === 'string' || typeof c === 'number') {
    return myVode(sel, data, undefined, c, undefined)
  } else if (Array.isArray(c)) {
    let children = []
    for (let i = 0; i < c.length; i++) {
      if (!(typeof c[i] === 'object' && c[i].hasOwnProperty('sel'))) {
        throw Error('数组中存在不是h函数')
      }
      children.push(c[i])
    }
    return myVode(sel, data, children, undefined, undefined)
  } else if (typeof c === 'object' && c.hasOwnProperty('sel')) {
    let children = [c]
    return myVode(sel, data, children, undefined, undefined)
  } else {
    throw Error('传入的参数格式不对')
  }
}
